import '../utils/tools.dart';
import 'article_model.dart';
import 'comparable.dart';
import 'serializable.dart';

class CategoryModel extends Serializable implements Equable<int> {
  int id = 0;
  int pid = 0;
  String title = '';
  String viceTitle = '';
  String short = '';
  String name = '';
  String icon = '';
  String image = '';
  String keywords = '';
  String description = '';

  int pageSize = 0;
  int sort = 0;

  bool isTop = false;
  bool isComment = false;
  bool isImages = false;
  bool isAttachments = false;
  int channelMode = 0;

  bool useTemplate = false;
  String templateDir = '';

  List<ArticleModel>? lists;

  CategoryModel();

  CategoryModel.fromJson(Map category) {
    id = Tools.parseInt(category['id']);
    pid = Tools.parseInt(category['pid']);
    title = category['title'] ?? '';
    viceTitle = category['vice_title'] ?? '';
    short = category['short'] ?? '';
    name = category['name'] ?? '';
    keywords = category['keywords'] ?? '';
    description = category['description'] ?? '';
    icon = Tools.fixImageUrl(category['icon']);
    image = Tools.fixImageUrl(category['image']);
    pageSize = Tools.parseInt(category['pagesize']);
    sort = Tools.parseInt(category['sort']);

    isTop = Tools.parseBool(category['is_top']);
    isImages = Tools.parseBool(category['is_images']);
    isAttachments = Tools.parseBool(category['is_attachments']);

    channelMode = Tools.parseInt(category['channel_mode']);
    useTemplate = Tools.parseBool(category['use_template']);
    templateDir = category['template_dir'] ?? '';

    if (category.containsKey('articles')) {
      lists = category['articles']
          .map<ArticleModel>((item) => ArticleModel.fromJson(item))
          .toList();
    }
  }

  static Map<int, List<CategoryModel>> mapTree(dynamic lists) {
    if (lists == null) return <int, List<CategoryModel>>{};

    if (lists is List<dynamic>) {
      return lists.asMap().map<int, List<CategoryModel>>((key, item) =>
          MapEntry(
              key,
              item
                  .map<CategoryModel>((cate) => CategoryModel.fromJson(cate))
                  .toList()));
    } else if (lists is Map<int, dynamic>) {
      return lists.map<int, List<CategoryModel>>((key, item) => MapEntry(
          key,
          item
              .map<CategoryModel>((cate) => CategoryModel.fromJson(cate))
              .toList()));
    } else if (lists is Map<String, dynamic>) {
      return lists.map<int, List<CategoryModel>>((key, item) => MapEntry(
          Tools.parseInt(key),
          item
              .map<CategoryModel>((cate) => CategoryModel.fromJson(cate))
              .toList()));
    } else {
      throw Exception('cates map error: $lists');
    }
  }

  @override
  int get compareField => id;

  @override
  Map toJson() {
    return {
      'id': id,
      'pid': pid,
      'title': title,
      'vice_title': viceTitle,
      'short': short,
      'name': name,
      'icon': icon,
      'image': image,
      'keywords': keywords,
      'description': description,
      'pagesize': pageSize,
      'sort': sort,
      'is_top': isTop,
      'is_comment': isComment,
      'is_images': isImages,
      'is_attachments': isAttachments,
      'channel_mode': channelMode,
      'use_template': useTemplate,
      'template_dir': templateDir,
    };
  }
}
